package com.rongshu.view.discovery.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.rongshu.R;
import com.rongshu.eventbus.ArticleSetRead;
import com.rongshu.eventbus.DiscoverySearchResult;
import com.rongshu.view.base.AdapterListener;
import com.rongshu.view.base.BaseFragment;
import com.rongshu.view.discovery.adapter.OtherArticleAdapter;
import com.rongshu.view.discovery.bean.ArticleListItemBean;
import com.rongshu.view.discovery.bean.ArticleListResponse;
import com.rongshu.view.discovery.contract.SearchArticleView;
import com.rongshu.view.discovery.presenter.SearchArticlePresenter;
import com.rongshu.view.home.bean.LabelResponse;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * created by zhengweis on 2018/11/1.
 * description：搜索文章
 */
public class SearchResultArticleFragment extends BaseFragment<SearchArticlePresenter> implements SearchArticleView {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refresh_view)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @BindView(R.id.no_data_img)
    ImageView noDataImg;

    private List<ArticleListItemBean> articleList;
    private OtherArticleAdapter adapter;
    private LabelResponse.ResponseBean bean;
    private int pageIndex = 1;
    private int pageSize = 10;
    private String msg;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_search_result_article;
    }

    /**
     * 接受输入框的信息
     *
     * @param data
     */
    @Subscribe
    public void onMessageEventPost(DiscoverySearchResult data) {
        pageIndex = 1;
        presenter.getData(data.getMsg(), pageIndex, pageSize);
    }


    /**
     * 同步已读文章
     */
    @Subscribe
    public void onMessageEventPost(ArticleSetRead data) {
        for (ArticleListItemBean bean : articleList) {
            if (bean.getContribute_id() == data.getContribute_id()) {
                bean.setRead(1);
            }
        }
        if (adapter != null) adapter.notifyDataSetChanged();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Override
    protected void initViews() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);
        articleList = new ArrayList<>();
        adapter = new OtherArticleAdapter(getActivity(), articleList);
        recyclerView.setAdapter(adapter);
        adapter.setAdapterListener(new AdapterListener() {
            @Override
            public void callBack(Object object) {
                ArticleListItemBean response = (ArticleListItemBean) object;
                presenter.toArticleDetailsAct(response);
            }
        });

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageIndex = 1;
                refreshLayout.resetNoMoreData();
                presenter.getData(bean.getTitle(), pageIndex, pageSize);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageIndex++;
                presenter.getData(bean.getTitle(), pageIndex, pageSize);
            }
        });
    }

    @Override
    protected void initData() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            bean = (LabelResponse.ResponseBean) bundle.getSerializable("searchMsg");
        }
        setPresenter(new SearchArticlePresenter(getActivity()), this);
        if (bean != null) {
            msg = bean.getTitle();
            presenter.getData(msg, pageIndex, pageSize);
        }
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @Override
    public void success(Object object, int tag) {
        ArticleListResponse response = (ArticleListResponse) object;
        if (response == null) {
            return;
        }
        if (pageIndex == 1) {
            articleList.clear();
        }
        if (refreshLayout.isLoading()) {
            refreshLayout.finishLoadmore();
        }
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh();
        }
        List<ArticleListItemBean> list = response.getResponse();
        articleList.addAll(list);
        adapter.notifyDataSetChanged();
        noDataLayout.setVisibility(articleList.size() == 0 ? View.VISIBLE : View.GONE);
        if (list.size() < pageSize) {
            refreshLayout.setLoadmoreFinished(true);
        }
    }

    @Override
    public void faild(Object object, int tag) {

    }
}
